import { http } from "@/utils/http";
import { LoginData, LoginResult, VerifyCodeResult } from "@/views/login/types";
import { AxiosPromise } from "axios";

const clientId = import.meta.env.VITE_APP_CLIENT_ID;

export function getGenerateCode(params): Promise<any> {
  return http.request({
    url: "/sys/getGenerateCode",
    method: "get",
    params
  });
}

export function doPostLogin(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password"
  };
  return http.request({
    url: "/auth/login",
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: "post",
    data: params
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return http.request({
    url: '/auth/code',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  });
}

export function authBinding(source: string, tenantId: string, domain: string) {
  return http.request({
    url: '/auth/binding/' + source,
    method: 'get',
    params: {
      tenantId,
      domain
    }
  });
}
